import { Controller, DynamicModule, Get } from "@nestjs/common";
import { LazyModuleLoader } from "@nestjs/core";
import { UsersModule } from "src/business/users/users.module";
import { UsersService } from "src/business/users/users.service";
import { Public } from "src/decorators/is-public.decorator";

@Controller('/lazy-loading')
export class lazyLoadingController {
    constructor(private readonly lazyModuleLoader: LazyModuleLoader) { }

    @Public()
    @Get('/lazy-users')
    async getLazyUsers() {
        // 不实用路径 import('../../business/users/user.module.ts')
        // 直接使用 UsersModule
        const moduleRef = await this.lazyModuleLoader.load(()=> UsersModule)
        const usersService: UsersService = moduleRef.get(UsersService);
        return usersService.findAllWithConvert()
    }

    @Public()
    @Get('/simulate-error')
    async simulateError() {
        throw new Error('Service Execution Error: Database Connection Failed');
    }
}